package com.nextar.ui.admin.doctor;

import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.nextar.ui.admin.domain.doctor.Doctor;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.themes.BaseTheme;

import fi.abo.GAEContainer.impl.GAEContainer;

public class DoctorForm extends VerticalLayout{

	/**
	 * 
	 */
	private static final long serialVersionUID = 7554754186536735875L;
	private static final Logger LOGGER = Logger.getLogger(DoctorForm.class.getName());
	
	Doctor doctorDomain;
	
	public DoctorForm() {
		doctorDomain = new Doctor();
		BeanItem<Doctor> doctorBeanItem = new BeanItem<Doctor>(doctorDomain);
		
		final Form doctorForm = new Form();
		doctorForm.setCaption("Doctor Details");
		doctorForm.setWriteThrough(false);
		doctorForm.setInvalidCommitted(false);
		
		doctorForm.setFormFieldFactory(new DoctorFieldFactory());
		doctorForm.setItemDataSource(doctorBeanItem);
		
		// Determines which properties are shown, and in which order:
		doctorForm.setVisibleItemProperties(Arrays.asList(new String[] {
                "name", "speciality", "area", "address",
                "dateOfVisit", "productsTobeFocused", "productsAtPresent", "rating" }));
		
		addComponent(doctorForm);
		
		// The cancel / apply buttons
        HorizontalLayout buttons = new HorizontalLayout();
        buttons.setSpacing(true);
        Button discardChanges = new Button("Discard changes",
                new Button.ClickListener() {
                    public void buttonClick(ClickEvent event) {
                        doctorForm.discard();
                    }
                });
        discardChanges.setStyleName(BaseTheme.BUTTON_LINK);
        buttons.addComponent(discardChanges);
        buttons.setComponentAlignment(discardChanges, Alignment.MIDDLE_LEFT);

        Button apply = new Button("Apply", new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                try {
                    doctorForm.commit();
                    //GAEContainer container = new GAEContainer(kind)
                } catch (Exception e) {
                    LOGGER.log(Level.SEVERE, "An error occured while saving data :", e);
                }
            }
        });
        buttons.addComponent(apply);
        doctorForm.getFooter().addComponent(buttons);
        doctorForm.getFooter().setMargin(false, false, true, true);

        // button for showing the internal state of the POJO
        Button showPojoState = new Button("Show POJO internal state",
                new Button.ClickListener() {				
                    public void buttonClick(ClickEvent event) {
                        showPojoState();
                    }
                });
        addComponent(showPojoState);
	}
	
	private void showPojoState() {
		String description = "First name: " + doctorDomain.getName()
				+ "<br/>Speciality: " + doctorDomain.getSpeciality()
				+ "<br/>Address: " + doctorDomain.getAddress() + "<br/>Area: "
				+ doctorDomain.getArea() + "<br/>Date Of visit"
				+ doctorDomain.getDateOfVisit()
				+ "<br/>Products to be focused:"
				+ doctorDomain.getProductsTobeFocused()
				+ "<br/>Products at present:"
				+ doctorDomain.getProductsAtPresent() + "<br/>Rating:"
				+ doctorDomain.getRating();
		Window.Notification n = new Window.Notification("POJO state",
				Window.Notification.TYPE_TRAY_NOTIFICATION);
		n.setPosition(Window.Notification.POSITION_CENTERED);
		n.setDescription(description);
		
		LOGGER.log(Level.INFO, "Details Entered :"+description);
		
		getWindow().showNotification(n);
	}
}
